package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜品分类
 *
 * @Author Yang
 * @Date 2023/3/8 17:16
 */
@Slf4j
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    // H5菜品查询

    @GetMapping("/dish/list")
    public ResultInfo list(Long categoryId, Integer status) {
        List<Dish> list = null;
        String dishKey = "dish_" + categoryId;
        if (redisTemplate.hasKey(dishKey)) {
            log.info("查询redis缓存");
            list = (List<Dish>) redisTemplate.opsForValue().get(dishKey);
        } else {
            list = dishService.list(categoryId, status);
            redisTemplate.opsForValue().set(dishKey, list);
            log.info("查询mysql缓存");
        }
        return ResultInfo.success(list);
    }
}
